package com.hirsi.common.core.vo.result;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hirsi.common.core.exception.ServerRuntimeException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@ApiSort(1)
@ApiModel("全局返回Body")
@NoArgsConstructor
public class ResponseResult<T> {

    @ApiModelProperty(value = "是否成功：true-是，false-否", required = true)
    private Boolean success;

    @ApiModelProperty(value = "状态码", required = true, position = 1)
    private Integer code;

    @ApiModelProperty(value = "提示消息", required = true, position = 2)
    private String msg;

    @ApiModelProperty(value = "数据", position = 3)
    private T data;

    public static <T> ResponseResult<T> success() {
        return success("请求成功");
    }

    public static <T> ResponseResult<T> success(String msg) {
        return success(msg, null);
    }

    public static <T> ResponseResult<T> success(T data) {
        return success("请求成功", data);
    }

    public static <T> ResponseResult<T> success(String msg, T data) {
        return new ResponseResult<>(true, 200, msg, data);
    }

    public static <T> ResponseResult<T> fail() {
        return fail(500);
    }

    public static <T> ResponseResult<T> fail(Integer code) {
        return fail(code, "服务器错误，请稍后重试");
    }

    public static <T> ResponseResult<T> fail(String msg) {
        return fail(500, msg);
    }

    public static <T> ResponseResult<T> fail(Integer code, String msg) {
        return fail(code, msg, null);
    }

    public static <T> ResponseResult<T> fail(Integer code, String msg, T data) {
        return new ResponseResult<>(false, code, msg, data);
    }

    public static  ResponseResult fail(ServerRuntimeException e) {
        return new ResponseResult<>(false, e.getCode(), e.getMessage(), e.getData());
    }

    public ResponseResult(Boolean success, Integer code, String msg, T data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
}
